import { ref, onMounted } from 'vue'
import { baseData } from './mock'
export function perFromModuleData() {
  // 轮播图初始数据
  const bannerDataList = ref([])
  // 轮播图
  const dataList = ref([])
  onMounted(() => {
    getDataList()
  })
  const getDataList = async () => {
    dataList.value = []
    bannerDataList.value = []
    try {
      // 使用临时数据
      let disposeData = []
      if (baseData.length > 0) {
        disposeData = baseData.map((item) => {
          return {
            ...item,
            nowNumTqRate:
              item.nowNumTqRate && item.nowNumTqRate.includes('%')
                ? item.nowNumTqRate.replace('%', '')
                : item.nowNumTqRate,
            hoverStatus: false,
            isAdd: false
          }
        })
      }
      disposeData.push({
        targetName: '新增菜单',
        isAdd: true,
        hoverStatus: false
      })
      bannerDataList.value = disposeData
      dataList.value = splitArrayIntoChunks(disposeData, 10)
    } catch (error) {
      throw new Error(error)
    }
  }
  function splitArrayIntoChunks(arr = [], chunkSize) {
    const result = []
    if (arr.length === 0) {
      return []
    }
    for (let i = 0; i < arr.length; i += chunkSize) {
      result.push(arr.slice(i, i + chunkSize))
    }
    return result
  }
  const handleMouseEnter = (row, flag) => {
    row.hoverStatus = flag
  }
  const goDetail = (row) => {
    console.log(row)
  }
  return {
    dataList,
    bannerDataList,
    handleMouseEnter,
    goDetail
  }
}
